<?php

namespace Store\Controller;

use Think\Controller;

class PromotionController extends CommonController
{

    public function coupontypelist()
    {
        $coupon_type = M('coupon_type');
        if ($_POST) {

        } else {
            $storeArray = $this->storeArray;
            $uid = $storeArray['id'];
            $coupontypelist = $coupon_type->where("store_id=$uid")->select();
        }

        $this->assign('coupontypelist', $coupontypelist);
        $this->display();

    }

    public function coupontypeadd()
    {
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        if ($_POST) {
            $data = I('post.');
            $add_info['store_id'] = $store_id;
            $add_info['coupon_name'] = $data['coupon_name'];
            $add_info['money'] = $data['money'];
            $add_info['count'] = $data['count'];
            $add_info['max_fetch'] = $data['max_fetch'];
            $add_info['at_least'] = $data['at_least'];
            $add_info['range_type'] = $data['range_type'];
            $add_info['start_time'] = $data['start_time'];
            $add_info['coupon_type'] = $data['coupon_type'];
            $add_info['end_time'] = $data['end_time'];
            $add_info['suit_goodIds'] = $data['ids'];
            $add_info['create_time'] = time();
            $add_info['update_time'] = time();
            $add_info['is_show'] = $data['navigationtype'];
            $coupon_type = M('coupon_type');
            $coupon_goods = M('coupon_goods');
            $coupon_id = $coupon_type->add($add_info);
            /*if($data['range_type']==1){
                $shop_ids=explode(',',$data['ids']);
                $ids=array();
                if(is_array($shop_ids)){
                    foreach ($shop_ids as $key=>$val){
                        $ids[$key]['id']=$val;
                    }
                }else{
                    $ids['0']['id']=$shop_ids;
                }
            }elseif($data['range_type']==2){
                $category=$data['category'];
                $ids=M('shop')->field('yt_shop.id')->where('type_id='.$category.' and store_id='.$store_id)->select();
            }elseif($data['range_type']==3){
                $ids=M('shop')->field('yt_shop.id')->where('store_id='.$store_id)->select();
            }*/
            if ($data['range_type'] == 0) {
                $shop_ids = explode(',', $data['ids']);
                $ids = array();
                if (is_array($shop_ids)) {
                    foreach ($shop_ids as $key => $val) {
                        $ids[$key]['id'] = $val;
                    }
                } else {
                    $ids['0']['id'] = $shop_ids;
                }
            } else {
                $ids = M('shop')->field('yt_shop.id')->where('store_id=' . $store_id)->select();
            }
            if (is_array($ids)) {
                foreach ($ids as $key => $value) {
                    $shop_coupon['coupon_type_id'] = $coupon_id;
                    $shop_coupon['goods_id'] = $value['id'] ? $value['id'] : $value;
                    $coupon_goods->add($shop_coupon);
                }
            }
            //生成优惠券
            $coupon = M('coupon');
            for ($i = 0; $i < $data['count']; $i++) {
                $coupon_xq['coupon_type_id'] = $coupon_id;
                $coupon_xq['store_id'] = $store_id;
                $coupon_xq['coupon_code'] = date("YmdHis", time()) . $i;
                $coupon_xq['money'] = $data['money'];
                $coupon_xq['start_time'] = $data['start_time'];
                $coupon_xq['end_time'] = $data['end_time'];
                $coupon->add($coupon_xq);
            }
            $this->ajax_r(1,"添加成功",array('url' => U('Promotion/coupontypelist')));
        } else {
            $this->display();
        }

    }

    function coupontypedit()
    {
        $id = I('get.id');
        $storeArray = $this->storeArray;
        $uid = $storeArray['id'];
        $coupon_type = M('coupon_type');
        $coupon_goods = M('coupon_goods');
        $coupon_info = $coupon_type->where("store_id=$uid and coupon_type_id=$id")->find();
        $coupon_g = $coupon_goods
            ->field('yt_shop.id,yt_shop.title,yt_shop.num,yt_shop.image,yt_shop.price')
            ->join('yt_shop on yt_shop.id=yt_coupon_goods.goods_id', 'left')
            ->where("yt_coupon_goods.coupon_type_id=$id and yt_shop.ishidden=0")
            ->select();
        foreach ($coupon_g as $key => $v) {
            $ids[$key] = $v['id'];
        }
        if ($coupon_g) {
            $coupon_g = json_encode($coupon_g);
            $ids = json_encode($ids);
        } else {
            $coupon_g = '';
            $ids = '';
        }
        $y_count_counpon = M('coupon')->where('uid !=0 and coupon_type_id=' . $id)->count();
        $this->assign('y_count_counpon', $y_count_counpon);
        $this->assign('coupon_info', $coupon_info);
        $this->assign('coupon_g', $coupon_g);
        $this->assign('ids', $ids);
        $this->display();
    }

    function update_couponty()
    {
        if ($_POST) {
            $storeArray = $this->storeArray;
            $store_id = $storeArray['id'];
            $data = I('post.');
            //已发放张数
            $count_counpon = M('coupon')->where('coupon_type_id=' . $data['coupon_type_id'])->count();
            if ($count_counpon) {
                //已领取
                $y_count_counpon = M('coupon')->where('uid !=0 and coupon_type_id=' . $data['coupon_type_id'])->count();
                if ($y_count_counpon > $data['count']) {
                    $this->ajax_r(0,"已被领取" . $y_count_counpon . "张，更新总张数不能低于" . $y_count_counpon . "张");
                } else {
                    if ($count_counpon > $data['count']) {
                        $del_num = $count_counpon - $data['count'];
                        M('coupon')->where('uid =0 and coupon_type_id=' . $data['coupon_type_id'])->order('coupon_id')->limit($del_num)->delete();
                    } else {
                        $add_num = $data['count'] - $count_counpon;
                        $coupon = M('coupon');
                        for ($i = 0; $i < $add_num; $i++) {
                            $coupon_xq['coupon_type_id'] = $data['coupon_type_id'];
                            $coupon_xq['store_id'] = $store_id;
                            $coupon_xq['coupon_code'] = date("YmdHis", time()) . $i;
                            $coupon_xq['money'] = $data['money'];
                            $coupon_xq['start_time'] = $data['start_time'];
                            $coupon_xq['end_time'] = $data['end_time'];
                            $coupon->add($coupon_xq);
                        }
                    }
                }
            }
            $add_info['coupon_type_id'] = $data['coupon_type_id'];
            $add_info['store_id'] = $store_id;
            $add_info['coupon_name'] = $data['coupon_name'];
            $add_info['money'] = $data['money'];
            $add_info['count'] = $data['count'];
            $add_info['max_fetch'] = $data['max_fetch'];
            $add_info['at_least'] = $data['at_least'];
            $add_info['range_type'] = $data['range_type'];
            $add_info['suit_goodIds'] = $data['ids'];
            $add_info['start_time'] = $data['start_time'];
            $add_info['end_time'] = $data['end_time'];
            $add_info['create_time'] = time();
            $add_info['update_time'] = time();
            $add_info['is_show'] = $data['navigationtype'];
            $coupon_type = M('coupon_type');
            $coupon_goods = M('coupon_goods');
            $coupon_type->save($add_info);
            //商品编辑
            $delete['coupon_type_id'] = $data['coupon_type_id'];
            $coupon_goods->where($delete)->delete();
            if ($data['range_type'] == 0) {
                $shop_ids = explode(',', $data['ids']);
                $ids = array();
                if (is_array($shop_ids)) {
                    foreach ($shop_ids as $key => $val) {
                        $ids[$key]['id'] = $val;
                    }
                } else {
                    $ids['0']['id'] = $shop_ids;
                }
            } else {
                $ids = M('shop')->field('yt_shop.id')->where('store_id=' . $store_id)->select();
            }
            if (is_array($ids)) {
                foreach ($ids as $key => $value) {
                    $shop_coupon['coupon_type_id'] = $data['coupon_type_id'];
                    $shop_coupon['goods_id'] = $value['id'];
                    $coupon_goods->add($shop_coupon);
                }
            }
            //时间编辑
            $edit_time['start_time'] = $data['start_time'];
            $edit_time['end_time'] = $data['end_time'];
            M('coupon')->where('coupon_type_id=' . $data['coupon_type_id'])->save($edit_time);
            $this->ajax_r(1,"更新成功");
        }
    }

    /**
     * 获取优惠券详情
     */
    public function getCouponTypeInfo()
    {
        $coupon_type = M('coupon_type');
        $coupon_type_id = $_POST['coupon_type_id'];

        $data = $coupon_type->where('coupon_type_id=' . $coupon_type_id)->find();
        $this->ajaxReturn($data);

    }


    public function getsearchgoodslist()
    {
        $shop = D('shop');
        $storeArray = $this->storeArray;
        $where = 'ishidden=0 and store_id=' . $storeArray['id'];
        $title = preg_replace('/\//', ' ', I('get.title'));
        if ($title) {
            $titleArray = explode(' ', $title);
            if ($titleArray) {
                foreach ($titleArray AS $key => $row) {
                    $where .= $row != '' ? ' AND title LIKE "%' . $row . '%"' : '';
                }
            }
        }

        $where .= I('get.id') !== '' ? ' AND id = ' . I('get.id') . '' : '';

        $array = $shop->field("id,title,price,image,num")->where($where)->order('id DESC')->limit(8)->select();
        $data['data'] = $array;
        $this->ajaxReturn($data);
    }

    public function Spellgroup()
    {
        $shop = M('shop');
        $storeArray = $this->storeArray;
        $uid = $storeArray['id'];
        $rs = $shop->where('ishidden=0 and tuanselect=1 and store_id=' . $uid)->select();
        $this->assign('rs', $rs);

        $this->display();
    }

    public function daiyan()
    {
        $storeArray = $this->storeArray;
        $uid = $storeArray['id'];
        $count_rs = M('order_goods')
            ->join('yt_users on yt_users.id=yt_order_goods.recommend_id')
            ->where("yt_order_goods.store_id=$uid and yt_order_goods.recommend_id<>''")
            ->group('yt_order_goods.recommend_id')
            ->select();
        $count = count($count_rs);
        $Page = new \Think\Page ($count, 15); // 实例化分页类 传入总记录数
        $Page->setConfig('header', '条记录');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $show = $Page->show(); // 分页显示输出
        $rs = M('order_goods')
            ->join('yt_users on yt_users.id=yt_order_goods.recommend_id')
            ->where("yt_order_goods.store_id=$uid and yt_order_goods.recommend_id<>''")
            ->group('yt_order_goods.recommend_id')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('page', $show);
        $this->assign('rs', $rs);
        $this->display();
    }

    public function daiyan_info()
    {
        $id = I('get.id');
        $storeArray = $this->storeArray;
        $uid = $storeArray['id'];

        $user_info = M('users')->find($id);
        $this->assign('user_info', $user_info);
        //代言次数
        $daiyan_num = get_count_daiyan($id, $uid);
        $succse_daiyan_num = get_count_succse_daiyan($id, $uid);
        //代言红包
        $daiyan_price = get_count_daiyan_prcie($id, $uid);
        //待入账
        $daiyan_dr_price = get_count_daiyan_dr_prcie($id, $uid);

        $this->assign('daiyan_num', $daiyan_num);
        $this->assign('succse_daiyan_num', $succse_daiyan_num);
        $this->assign('daiyan_price', $daiyan_price);
        $this->assign('daiyan_dr_price', $daiyan_dr_price);
        $count = M('order_goods')
            ->join('yt_users on yt_users.id=yt_order_goods.recommend_id')
            ->where("yt_order_goods.store_id=$uid and yt_order_goods.recommend_id=$id")
            ->count();

        $Page = new \Think\Page ($count, 15); // 实例化分页类 传入总记录数
        $Page->setConfig('header', '条记录');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $show = $Page->show(); // 分页显示输出
        $rs = M('order_goods')
            ->join('yt_order ON yt_order_goods.order_id = yt_order.order_id', 'left')
            ->field("yt_order.buyer_name,yt_order.order_sn,yt_order.add_time,yt_order.order_state,yt_order.refund_state,yt_order_goods.*")
            ->where("yt_order_goods.recommend_id=$id and yt_order_goods.store_id=$uid")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('yt_order_goods.rec_id desc')
            ->select();
        $this->assign('page', $show);
        $this->assign('rs', $rs);
        $this->display();
    }


}
